package com.lizhi.menu.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lizhi.menu.Dto.LoginDto;
import com.lizhi.menu.Dto.RegisterDto;
import com.lizhi.menu.common.response.Result;
import com.lizhi.menu.domain.SystemUser;
import com.lizhi.menu.service.ISystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户信息Controller
 *
 * @author lizhi
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/system/user")
@Api(tags = "用户管理")
public class SystemUserController {
    @Autowired
    private ISystemUserService systemUserService;

    /**
     * 查询用户信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询用户信息列表")
    public Result list(SystemUser systemUser) {
        List<SystemUser> list = systemUserService.selectSystemUserList(systemUser);
        return Result.ok(list);
    }

    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取用户信息详细信息")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.ok(systemUserService.selectSystemUserById(id));
    }

    /**
     * 新增用户信息
     */
    @PostMapping
    @ApiOperation("新增用户信息")
    public Result add(@RequestBody SystemUser systemUser) {
        systemUserService.insertSystemUser(systemUser);
        return Result.ok();
    }

    /**
     * 修改用户信息
     */
    @PutMapping
    @ApiOperation("修改用户信息")
    public Result edit(@RequestBody SystemUser systemUser) {
        systemUserService.updateSystemUser(systemUser);
        return Result.ok();
    }

    /**
     * 删除用户信息
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除用户信息")
    public Result remove(@PathVariable Long[] ids) {
        systemUserService.deleteSystemUserByIds(ids);
        return Result.ok();
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody @Validated LoginDto loginDto){
        SystemUser systemUser = new SystemUser().selectOne(Wrappers.<SystemUser>lambdaQuery()
                .eq(SystemUser::getUsertel, loginDto.getTel()).eq(SystemUser::getPwd, loginDto.getPwd()).last("limit 1"));
        if (ObjectUtils.isEmpty(systemUser)) {
            return Result.failure("用戶名或者密碼錯誤");
        }
        StpUtil.login(systemUser.getId());
        return Result.ok(StpUtil.getTokenValue());
    }

    @PostMapping("/logOut")
    @ApiOperation("登出")
    public Result logOut(){
        StpUtil.logout();
        return Result.ok();
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public Result register(@RequestBody RegisterDto dto){
        SystemUser systemUser = new SystemUser().selectOne(Wrappers.<SystemUser>lambdaQuery()
                .and(user -> user
                        .eq(SystemUser::getUsername, dto.getUsername()).or()
                        .eq(SystemUser::getUsertel, dto.getUsertel()))
                .last("limit 1"));
        if (!ObjectUtils.isEmpty(systemUser)) {
            return Result.failure("用户名称或手机号存在");
        }
        systemUser = new SystemUser();
        systemUser.setUsername(dto.getUsername());
        systemUser.setUsertel(dto.getUsertel());
        systemUser.setPwd(dto.getPwd());
        if (!ObjectUtils.isEmpty(dto.getUserType())){
            systemUser.setUserType(dto.getUserType());
        }else {
            systemUser.setUserType(2);
        }

        if (!ObjectUtils.isEmpty(dto.getAvatar())){
            systemUser.setAvatar(dto.getAvatar());
        }
        systemUser.insert();
        StpUtil.login(systemUser.getId());
        return Result.ok();
    }
}
